--- 模块功能：MQTT客户端处理框架
-- @author openLuat
-- @module mqtt.mqttTask
-- @license MIT
-- @copyright openLuat
-- @release 2018.03.28

module(...,package.seeall)

require"misc"
require"mqtt"
require"mqttOutMsg"
require"mqttInMsg"

local ready = false
local versionn="1.0"
--- MQTT连接是否处于激活状态
-- @return 激活状态返回true，非激活状态返回false
-- @usage mqttTask.isReady()
function isReady()
    return ready
end
local relayStatus=0  --继电器状态
local ds,dd,df="","",""
local IMEI,topic1,topic2,topic3,topic4
local  mqttClient
--启动MQTT客户端任务

function get_Status()
    return relayStatus
end
function set_Status(status)
    relayStatus=status
end
function  get_IMEI()
    return IMEI
end

sys.taskInit(
    function()
        
        local retryConnectCnt = 0
        while true do
            if not socket.isReady() then
                retryConnectCnt = 0
                --等待网络环境准备就绪，超时时间是5分钟
                sys.waitUntil("IP_READY_IND",300000)
            end
           if socket.isReady() then
            IMEI = misc.getImei()
            --IMEI="863488052352472"
                --创建一个MQTT客户端
                df=string.format( "{\"deviceNum\": \"%s\",\"isOnline\": 0}",IMEI )
                mqttClient = mqtt.client(IMEI,30,"admin","admin123",1,{qos=0, retain=0, topic="offline", payload=df})
                --阻塞执行MQTT CONNECT动作，直至成功
               
                --如果使用ssl连接，打开mqttClient:connect("lbsmqtt.airm2m.com",1884,"tcp_ssl",{caCert="ca.crt"})，根据自己的需求配置
                --mqttClient:connect("lbsmqtt.airm2m.com",1884,"tcp_ssl",{caCert="ca.crt"})
                if mqttClient:connect("51xy.xyz",1883,"tcp") then
                    log.info("连接")
                    retryConnectCnt = 0
                    ready = true
                    
                    ds=string.format( "{\"deviceNum\":\"%s\",\"categoryId\":4,\"firmwareVersion\":\"%s\",\"ownerId\":\"1\"}",IMEI,versionn)
                    local s="ss"..IMEI
                   dd=string.format( "{\"deviceNum\":\"%s\",\"relayStatus\":%d,\"isOnline\":1}",IMEI,relayStatus)
                    --mqttClient:publish("device_info",ds)
                    mqttOutMsg.insertMsg("device_info",ds)
                    mqttOutMsg.insertMsg("status",dd)
                    topic1=string.format( "status/set/%s",IMEI )
                    topic2=string.format( "status/get/%s",IMEI )
                    topic3=string.format( "setting/set/%s",IMEI )
                    topic4=string.format( "setting/get/%s",IMEI )
                    --订阅主题
                    if mqttClient:subscribe({[topic1]=0, [topic2]=1}) then
                       
                      
                        --循环处理接收和发送的数据
                        while true do
                            --if not mqttInMsg.proc(mqttClient,topic1,topic2) then log.error("mqttTask.mqttInMsg.proc error") break end
                            
                           mqttOutMsg.proc(mqttClient)
                           mqttInMsg.proc(mqttClient,topic1,topic2) 

                        
                        end
                      
                    end
                    ready = false
                else
                    retryConnectCnt = retryConnectCnt+1
                end
                --断开MQTT连接
                mqttClient:disconnect()
                if retryConnectCnt>=5 then link.shut() retryConnectCnt=0 end
                sys.wait(5000)
            else
                --进入飞行模式，20秒之后，退出飞行模式
                net.switchFly(true)
                sys.wait(20000)
                net.switchFly(false)
            end
        end
    end
)
